﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GolfLeague.EditModels;

namespace GolfLeague.Models
{
    public class TeamInterface
    {
        GolfLeagueEntities golfDB = new GolfLeagueEntities();

        public IQueryable<Team> GetAllTeams()
        {
            return from teamEntity in golfDB.Teams
                   orderby teamEntity.TeamName
                   select teamEntity;
        }

        public IQueryable<Team> GetAllActiveTeams()
        {
            DateTime? endTime = null;
            return from teamEntity in golfDB.Teams
                   where teamEntity.EndedOn.Equals(endTime)
                   orderby teamEntity.TeamName
                   select teamEntity;
        }

        public Team GetTeam(int teamID)
        {
            var teamQuery = from teamEntity in golfDB.Teams
                            where teamEntity.TeamID.Equals(teamID)
                            select teamEntity;

            return teamQuery.FirstOrDefault();
        }

        public IQueryable<Player> GetTeamPlayers(int teamID)
        {
            return from teamEntity in golfDB.PlayerTeams
                   join playerEntity in golfDB.Players
                   on teamEntity.PlayerID equals playerEntity.PlayerID
                   where teamEntity.TeamID.Equals(teamID)
                   select playerEntity;
        }

        public PlayerTeam GetPlayerTeam(int playerTeamID)
        {
            return (from playerEntity in golfDB.PlayerTeams
                    where playerEntity.PlayerTeamID.Equals(playerTeamID)
                    select playerEntity).FirstOrDefault();
        }

        public PlayerTeam GetPlayerTeam(int playerID, int teamID)
        {
            return (from playerEntity in golfDB.PlayerTeams
                    where playerEntity.PlayerID.Equals(playerID) && playerEntity.TeamID.Equals(teamID)
                    select playerEntity).FirstOrDefault();
        }

        public void Add(Team team)
        {
            golfDB.Teams.AddObject(team);
        }

        public void AddPlayerTeam(PlayerTeam playerTeam)
        {
            golfDB.PlayerTeams.AddObject(playerTeam);
        }

        public void Delete(Team team)
        {
            golfDB.Teams.DeleteObject(team);
            
        }

        public void DeletePlayerTeam(PlayerTeam playerTeam)
        {
            golfDB.PlayerTeams.DeleteObject(playerTeam);
        }

        public void Save()
        {
            golfDB.SaveChanges();
        }

        #region Edit/View Model Builders

        public TeamEditModel GetTeamEditModel()
        {
            PlayerInterface playerInterface = new PlayerInterface();
            List<StandardDropdown> playerDropdown = new List<StandardDropdown>();
            foreach (Player player in playerInterface.GetAllPlayers())
                playerDropdown.Add(new StandardDropdown
                {
                    dropdownID = player.PlayerID,
                    dropdownText = player.FirstName + " " + player.LastName
                });

            return new TeamEditModel
            {
                Teams = GetAllTeams(),
                NewTeam = new Team(),
                PlayerA = new PlayerTeam(),
                PlayerB = new PlayerTeam(),
                Players = playerDropdown
            };
        }

        public TeamEditModel GetTeamEditModel(int teamID)
        {
            TeamEditModel teamEditModel = GetTeamEditModel();
            teamEditModel.NewTeam = GetTeam(teamID);
            IEnumerable<Player> teamPlayers = GetTeamPlayers(teamID);
            teamEditModel.PlayerA = GetPlayerTeam(teamPlayers.First().PlayerID, teamID);
            teamEditModel.PlayerB = GetPlayerTeam(teamPlayers.Last().PlayerID, teamID);
            return teamEditModel;
        }

        #endregion
    }
}